import request from './request'
import { useUserStore } from '../store/modules/user'
import { AxiosPromise } from 'axios'

/**
 * 获取用户列表
 * @param data - 请求参数
 * @param data.levelId - 用户级别ID
 * @param data.limit - 每页显示数量
 * @param data.page - 当面页码
 * @param data.status - 用户状态：0-正常，1-冻结，2-禁用
 * @param data.userContactSearch - 联系人搜索关键词：联系人姓名/手机号
 * @param data.userSearch - 账号关键词搜索：客户编码/账号昵称/登录账号
 * @param data.userTypeId - 用户分类ID
 */
export function getUsersList(
  data: UserAPIRequest.GetUserListParam,
): AxiosPromise<UserAPIResponse.ListItem> {
  return request({
    url: '/user/getList',
    method: 'post',
    data,
  })
}

// 导出公司数据
export function userExportExcel(data: any) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/dataExport/userExportExcel',
    method: 'post',
    headers: token,
    data,
    responseType: 'blob',
  })
}

// 新增或修改用户
export function addUser(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/add',
    method: 'post',
    headers: token,
    data,
  })
}

// 批量更改用户状态
export function changeStatus(data: object, status: boolean) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/status',
    method: 'put',
    headers: token,
    data,
    params: { status },
  })
}

// 批量删除用户
export function deleteUser(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/',
    method: 'delete',
    headers: token,
    data,
  })
}

// 分页获取用户级别下拉数据列表
export function levelPageSelect(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/select/user/levelPageSelect',
    method: 'post',
    headers: token,
    data,
  })
}

// 分页获取用户分类下拉数据列表
export function typePageSelect(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/select/user/typePageSelect',
    method: 'post',
    headers: token,
    data,
  })
}

/**
 * 获取用户账号信息
 * @param {String} id - 用户账号id
 */
export function getUserInfo(id: string): AxiosPromise<CustomerAPIResponse.Detail> {
  return request({
    url: '/user/' + id,
    method: 'get',
  })
}

/**
 * 根据账号id设置黑名单
 * @param {Array<string>} ids - 公司id集合
 * @param {Boolean} blacklist - 是否为黑名单
 */
export const setCustomerBlackList = (
  ids: Array<string>,
  blacklist: boolean,
): AxiosPromise<APIResponse> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/updateUserblacklist',
    method: 'post',
    headers: token,
    data: ids,
    params: {
      blacklist,
    },
  })
}

/**
 * 获取用户联系人列表
 * @param data - 请求参数
 * @param data.limit - 每页显示数量
 * @param data.name - 用户名称，支持模糊查询
 * @param data.page - 当面页码
 * @param data.phone - 用户手机号，支持模糊查询
 * @param id - 用户id
 */
export function getUserContact(
  data: {
    limit: number
    name: string
    page: number
    phone: string
  },
  id: string,
): AxiosPromise<APIResponseList<UserAPIResponse.UserContactPersonListItem>> {
  return request({
    url: '/user/contact/getUserContact/' + id,
    method: 'post',
    data,
  })
}

// 新增或修改联系人
export function saveUserContact(data: any, id: any) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/contact/save/' + id,
    method: 'post',
    headers: token,
    data,
  })
}

// 更改主要联系人
export function changeContact(id: any, userId: any) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/contact/' + userId,
    method: 'put',
    headers: token,
    params: { id },
  })
}

// 批量删除联系人
export function deleteContact(data: any) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/contact/',
    method: 'delete',
    headers: token,
    data,
  })
}

// 更新公司知识库查看权限
export function updateSelectKnowledgeAuthor(id: any, params: any) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/user/updateSelectKnowledgeAuthor/${id}`,
    method: 'PUT',
    headers: token,
    params,
  })
}

/**
 * 根据公司分类ID获取知识库类型权限列表
 * @param userTypeId 公司分类ID
 * @param data 搜索对象
 * @returns
 */
export function getSelectKnowledgeAuthorList(userTypeId: any, data: any) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/user/type/knowledge/type/getList/${userTypeId}`,
    method: 'post',
    headers: token,
    data,
  })
}

/* -----------基础资料相关  start ---------------  */

// 获取用户设置
export function getUserSet() {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/set/getUserSet',
    method: 'get',
    headers: token,
  })
}

// 保存用户设置
export function saveUserSet(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/set/saveUserSet',
    method: 'put',
    headers: token,
    data,
  })
}

// 获取用户级别列表
export function getLevelList(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/level/getList',
    method: 'post',
    headers: token,
    data,
  })
}

// 新增或修改用户级别
export function saveUserLevel(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/level/save',
    method: 'post',
    headers: token,
    data,
  })
}

// 批量更改用户级别状态
export function changeLevel(data: object, status: boolean) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/level/',
    method: 'put',
    headers: token,
    data,
    params: { status },
  })
}

// 批量删除用户级别状态
export function deleteLevel(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/level/',
    method: 'delete',
    headers: token,
    data,
  })
}

// 获取用户分类列表
export function getTypeList(params: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/type/getList',
    method: 'post',
    headers: token,
    data: params,
  })
}

// 新增或修改用户分类
export function saveUserType(params: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/type/save',
    method: 'post',
    headers: token,
    params,
  })
}

// 批量更改用户分类状态
export function changeType(data: object, status: boolean) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/type/',
    method: 'put',
    headers: token,
    data,
    params: { status },
  })
}

// 批量删除用户分类
export function deleteType(data: object) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/type/',
    method: 'delete',
    headers: token,
    data,
  })
}

/* -----------基础资料相关  end ---------------  */
//根据工单ID获取公司关联信息
export function getUserRelationInfo(workId) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/getUserRelationInfo/' + workId,
    method: 'get',
    headers: token,
  })
}

/**
 * 获取公司关联的条码产品列表
 * @param {Object} data - 请求参数
 * @param {Number} data.limit - 一页显示多少条
 * @param {Number} data.page - 页码
 * @param {String} userId - 公司id
 */
export function getUserRelationCodeGoodsList(
  data: { limit: number; page: number },
  userId: string,
): AxiosPromise<CustomerAPIResponse.ContactGoodList> {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/getUserRelationCodeGoodsList/' + userId,
    method: 'post',
    headers: token,
    data,
  })
}

// 获取公司关联的工单列表
export function getUserRelationOrderList(data: object, userId) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/getUserRelationOrderList/' + userId,
    method: 'post',
    headers: token,
    data,
  })
}
// 根据公司ID获取公司附件
export function getUserAnnex(userId) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/annex/getUserAnnex/' + userId,
    method: 'get',
    headers: token,
  })
}
//新增或删除用户附件
export function annexSave(data, userId) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/user/annex/save/' + userId,
    method: 'post',
    headers: token,
    data,
  })
}
//根据知识库分类ID获取说明书
export function getInstructionBook(knowledgeTypeId) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/knowledge/instruction-book/getInstructionBook/' + knowledgeTypeId,
    method: 'get',
    headers: token,
  })
}
//根据知识库分类ID获取故障库
export function getFailt(knowledgeTypeId) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/knowledge/fault/getFailt/' + knowledgeTypeId,
    method: 'get',
    headers: token,
  })
}
//根据知识库分类ID获取视频
export function getVideo(knowledgeTypeId) {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: '/knowledge/video/getVideo/' + knowledgeTypeId,
    method: 'get',
    headers: token,
  })
}

/**
 * 根据条码ID加入或解除黑名单
 * @param {String} snId - 条码id
 * @param {Boolean} blacklist - 是否加入黑名单
 */
export const updateBlackListSetBySnCodeId = (
  snId: string,
  blacklist: boolean,
): AxiosPromise<APIResponse> => {
  const token = {
    'access-token': useUserStore().token,
  }
  return request({
    url: `/code/updateCodeBlacklist/${snId}`,
    method: 'post',
    headers: token,
    params: {
      blacklist,
    },
  })
}

/**
 * 批量修改授信额度
 * @param {UserAPIRequest.UpdateUserCreditLimitForm} data - 请求参数
 * @param {number} data.creditLimit - 信用额度
 * @param {(1|2|3)} data.creditLimitType - 信用额度类型：1-空值，不限制，2-现付(0表示现付)，3-大于0，表示允许的额度
 * @param {number} data.settlementDeadline - 结算期限
 * @param {Array<string>} data.userIds - 公司
 */
export const multipleUpdateUserCreditLimit = (
  data: UserAPIRequest.UpdateUserCreditLimitForm,
): AxiosPromise<APIResponse> => {
  return request({
    url: '/user/updateUserCreditLimit',
    method: 'post',
    data,
  })
}

/**
 * 获取公司授信额度（用于创建工单时判断是否允许创建）
 * @param {string} userId - 公司id
 */
export const getUserCreditLimitByOrderTip = (
  userId: string,
): AxiosPromise<UserAPIResponse.CreditLimitByOrderTip> => {
  return request({
    url: `/user/getUserCreditLimit/${userId}`,
    method: 'get',
  })
}

/**
 * 获取公司主联系人
 * @param {UserAPIRequest.GetMainContactPersonOptionListParam} data - 请求参数
 * @param {string} data.id - id搜索
 * @param {number} data.limit - 每页显示数量
 * @param {string} data.name - 搜索关键词
 * @param {number} data.page - 当面页码
 */
export const getUserMainContactPersonOptionList = (
  data: UserAPIRequest.GetMainContactPersonOptionListParam,
): AxiosPromise<UserAPIResponse.MainContactPersonOptionList> => {
  return request({
    url: '/select/user/page/getUserList',
    method: 'post',
    data,
  })
}
